<template>
	<view class="house">
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="发布" :autoBack="true">
			</u-navbar>
		</view>
		<view class="con-body" style="padding-left: 20rpx; padding-right: 20rpx; background-color: #F8F8F8;">
			<!-- 发布 -->
			<view class="uni-flex-center">
				<view class="main-service uni-cell-90">
					<view class="user-list">
						<u-form>
							<view style="padding: 20rpx 30rpx;">

								<u-textarea v-model="content" :border="false" placeholder="品牌型号，新旧程度，入手渠道，转手原因...">
								</u-textarea>
								<view style="margin-top: 10rpx;">
									<text style="color: #007AFF;">{{topicTitle}}</text>
								</view>
								<u-line></u-line>
							</view>
							<view style="margin-top: 20rpx;margin-left: 40rpx;">
								<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
									multiple :maxCount="9"></u-upload>
							</view>

							<view style="padding: 20rpx 30rpx;">
								<u-form-item label="卖价" labelWidth="70" borderBottom>
									<u-input v-model="amount" placeholder="只做参考价价格" border="none"
										style="background-color: #FFFFFF;">
										<template slot="suffix">
											元
										</template>
									</u-input>
								</u-form-item>
								<u-form-item label="位置" labelWidth="70" borderBottom>
									<u-input v-model="address" placeholder="所在位置" border="none"
										style="background-color: #FFFFFF;">

									</u-input>
								</u-form-item>
								<u-form-item label="显示联系方式" labelWidth="100" borderBottom>
									<view style="text-align: right;">
										<u-switch v-model="is_contact" @change="changeContact"></u-switch>
									</view>

								</u-form-item>
								<view style="padding: 20rpx 0rpx;" v-if="is_contact == true">
									
										<view slot="prefix">
											<u-radio-group v-model="type" placement="row" @change="groupChange">
												<u-radio :customStyle="{marginLeft: '10rpx'}"
													v-for="(item, index) in contactList" :key="index"
													:label="item.label" :name="item.name" @change="radioChange">
												</u-radio>
											</u-radio-group>
										</view>
										<view style="margin-top:20rpx;">
											<text style="display:block;font-size: 26rpx;color:red;margin-bottom:20rpx">※微信号或电话号码必填</text>
											<u-input v-model="contact" placeholder="联系方式" border="none"
												style="background-color: #FFFFFF;"></u-input>
										</view>
										
								</view>
								<u-line></u-line>
							</view>


							<view class="user-list" style="margin-top: 20rpx;">
								<view
									style="padding: 15rpx 20rpx; text-align: center; font-size: 24rpx; float: left; height: 40rpx;"
									v-for="(item,index) in themesList">
									<view v-if="current == item.id" class="btn-view" @click="topic(item.id, item.name)">
										<text># {{item.name}} #</text>
									</view>
									<view v-else class="btn-view1" @click="topic(item.id, item.name)">
										<text># {{item.name}} #</text>
									</view>
								</view>
								<view style="clear: both; height: 80rpx;"></view>
							</view>
						</u-form>

					</view>
				</view>
			</view>
		</view>
		<view style="margin: 20rpx;">
			<u-alert v-if="showAlert == true" type="error" description="请先勾选同意平台使用协议"></u-alert>
			<u-checkbox-group @change="checkboxChange">
				<u-checkbox v-model="agree" shape="circle"></u-checkbox>
				<text style="font-size: 24rpx;">我已阅读并同意 <text style="color: #0B868E;">平台使用协议</text></text>
			</u-checkbox-group>
		</view>
		<u-toast ref="uToast"></u-toast>
		<u-button :customStyle="btnStyle" @click="release">发布</u-button>
	</view>
</template>

<script>
	import {
		getTopic, release
	} from "@/service/shareService.js";
	export default {
		data() {
			return {
				btnStyle: {
					width: '96%',
					borderRadius: '40rpx',
					marginLeft: '2%',
					marginTop: '20rpx',
					backgroundImage: "url('https://resourse.cnlhjt.com/upload/20220825/d1aa35c1d129137d9b12c8f34e59c5bc.png')",
					backgroundSize: '100% 80rpx',
					color: '#FFFFFF',
				},
				topicTitle: '',
				content: '',
				amount: '',
				address: '',
				is_contact: false,
				contact: '',
				type: 0,
				current: 0,
				themesList: [],
				fileList1: [],
				images: [],
				contact_type: 0,
				contactList: [{
						name: 0,
						label: '微信号',
						disabled: false
					},
					{
						name: 1,
						label: '电话号码',
						disabled: false
					}
				],

				agree: false,
				showAlert: false,
			}
		},
		onLoad() {
			getTopic().then(this.getTopic);
		},
		methods: {
			getTopic(e) {
				if (e.code == 200) {
					this.themesList = e.result;
				}
			},
			topic(tid, name) {
				this.current = tid;
				this.topicTitle = '# ' + name + ' #';
			},
			changeContact(e) {
				this.is_contact = e;

				if (this.is_contact) {
					this.contact_type = 1;
				} else {
					this.contact_type = 0;
				}
			},
			checkboxChange(e) {
				if (e.length == 1) {
					this.agree = true;

					this.showAlert = false;
				} else {
					this.agree = false;

					this.showAlert = true;
				}
			},
			groupChange(n) {
				this.contact_type = n+1;
			},
			radioChange(n) {
				this.contact_type = n+1;
			},
			release() {
				if (this.agree == false) {
					this.showAlert = true;
					return;
				}
				if(this.is_contact==true && this.contact==''){
					this.$refs.uToast.show({
						type: 'error',
						title: '联系方式还未填写',
						message: '联系方式还未填写',
						complete() {
							// uni.navigateBack({
							// 	delta: 1
							// })
						}
					})
					return;
				}
				let par = {
					topic: this.current,
					from: '共享格子铺',
					content: this.content,
					price: this.amount,
					location: this.address,
					images: this.images,
					contact_type: this.contact_type,
					contact: this.contact
				}
				
				release(par).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({
					
							type: 'success',
							title: '成功',
							message: res.msg,
							complete() {
								// uni.navigateBack({
								// 	delta: 1
								// })
								let pages = getCurrentPages();
								let beforePage = pages[pages.length - 2];
								beforePage.$vm.reFresh = Math.random()
								uni.navigateBack();
							}
						})
					} else {
						this.$refs.uToast.show({
					
							type: 'error',
							title: '失败',
							message: res.msg,
							complete() {
								// uni.navigateBack({
								// 	delta: 1
								// })
							}
						})
					}
				})

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.images.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://admin.gslhzhwy.com/api/upload/file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'smart-property'
						},
						success: (res) => {
							let par = JSON.parse(res.data);
							if (par.code == 1) {
								this.images.push(par.data.url);
							}
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	.btn-view {
		color: #FFFFFF;
		background-image: url("https://s4.ax1x.com/2022/02/22/HxzlJf.png");
		background-size: 100% 60rpx;
		border-radius: 30rpx;
		padding: 2rpx 15rpx;
	}

	.btn-view1 {
		color: #666666;
		border: #666666 1rpx solid;
		border-radius: 30rpx;
		padding: 2rpx 15rpx;
	}
</style>
